package com.wrupple.muba.catalogs.client.activity.process.task.impl;

import java.util.Map;

import com.google.gwt.user.cellview.client.CellTable;
import com.google.inject.Inject;
import com.wrupple.muba.catalogs.client.activity.process.task.GenericSelectionState;
import com.wrupple.muba.catalogs.client.module.services.logic.CatalogDescriptionService;
import com.wrupple.muba.catalogs.client.module.services.logic.FieldDescriptionService;
import com.wrupple.muba.catalogs.client.module.services.logic.GenericDataProvider;
import com.wrupple.muba.catalogs.client.module.services.presentation.GenericTableInitializer;
import com.wrupple.muba.catalogs.client.module.services.presentation.impl.CellTableFilterAndSortDelegate;
import com.wrupple.muba.catalogs.client.view.GenericBrowseView;
import com.wrupple.muba.catalogs.domain.JsCatalogKey;
import com.wrupple.muba.widget.client.widgets.table.TableWithPager;
import com.wrupple.vegetate.domain.CatalogDescriptor;
import com.wrupple.vegetate.domain.FieldDescriptor;

public class GenericSelectionStateImpl extends FilterableCatalogSelectionState<JsCatalogKey> implements GenericSelectionState{
	
	
	GenericTableInitializer tableInitializer;
	
	@Inject
	public GenericSelectionStateImpl(GenericBrowseView view,
			GenericTableInitializer tableInitializer, GenericDataProvider provider, CatalogDescriptionService catalogDescriptor,FieldDescriptionService fieldDescriptor, CellTableFilterAndSortDelegate filterAndSortDelegate) {
		super(view,provider, catalogDescriptor, fieldDescriptor,filterAndSortDelegate);
		this.tableInitializer=tableInitializer;
		filterAndSortDelegate.initialize(((TableWithPager)view.getDisplay()).getTable(), provider);
	}

	public GenericBrowseView getBrowseView(){
		return (GenericBrowseView) view;
	}
	
	
	protected void onCatalogLoaded(CatalogDescriptor catalog) {
		Map<String, FieldDescriptor> summaryFields = this.fieldDescriptor.getSummaryDescriptors(catalog);
		CellTable<JsCatalogKey> display =((TableWithPager)view.getDisplay()).getTable();
		tableInitializer.initialize(catalog, summaryFields, display );
		super.onCatalogLoaded(catalog);
	}
	
}
